package com.shangze.test.ui.fragment;

import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.shangze.test.R;
import com.shangze.test.base.BaseFragment;
import com.shangze.test.bean.UserBean;
import com.shangze.test.presenter.fragment.CircleFriendsPresenter;
import com.shangze.test.presenter_view.ArrayView;
import com.shangze.test.ui.adapter.CircleFriendsAdapter;
import com.shangze.test.utils.LogTool;
import com.shangze.test.view.MyLoadMoreView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * @name yjm
 * @time 2017/10/19 11:35
 * 朋友圈
 */
public class CircleFriendsFragment extends BaseFragment<CircleFriendsPresenter> implements ArrayView, SwipeRefreshLayout.OnRefreshListener, BaseQuickAdapter.RequestLoadMoreListener {

    @BindView(R.id.recycle_view)
    RecyclerView recycleView;
    @BindView(R.id.swipe_refresh_widget)
    SwipeRefreshLayout swipeRefreshWidget;
    Unbinder unbinder;
    CircleFriendsAdapter adapter;
    List<UserBean> userBeanList;
    int currentPage;
    Boolean isRefresh = true;

    public static CircleFriendsFragment newInstance() {
        CircleFriendsFragment fragment = new CircleFriendsFragment();
        Bundle args = new Bundle();
        fragment.setArguments(args);
        return fragment;
    }

    public CircleFriendsFragment() {
        // Required empty public constructor
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Override
    public void initView() {
        setTitleContent("朋友圈");
        left_toolbar.setVisibility(View.GONE);
        userBeanList = new ArrayList<>();
        recycleView.setLayoutManager(new LinearLayoutManager(getActivity()));
        adapter = new CircleFriendsAdapter(getActivity(), userBeanList);
        swipeRefreshWidget.setOnRefreshListener(this);
        //默认没数据时显示
        adapter.bindToRecyclerView(recycleView);
        adapter.setEmptyView(R.layout.view_emptyview);
        adapter.setOnLoadMoreListener(this, recycleView);
        adapter.setLoadMoreView(new MyLoadMoreView());
        recycleView.swapAdapter(adapter, true);
        presenter.getFriends(getActivity());
    }

    @Override
    protected int provideContentViewId() {
        return R.layout.fragment_circle_friends;
    }

    @Override
    public CircleFriendsPresenter createPresenter() {
        return new CircleFriendsPresenter();
    }

    @Override
    public void addNews(List data, int count) {
        LogTool.i("数据：" + data.toString());

        if (isRefresh) {
            adapter.setNewData(data);
        } else {
            currentPage = currentPage + 1;
            adapter.addData(data);
            if (data.size() >= 10) {
                adapter.loadMoreEnd();
            } else {
                adapter.loadMoreComplete();
            }
        }
        swipeRefreshWidget.setRefreshing(false);

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // TODO: inflate a fragment view
        View rootView = super.onCreateView(inflater, container, savedInstanceState);
        unbinder = ButterKnife.bind(this, rootView);
        return rootView;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @Override
    public void onRefresh() {
        currentPage = 0;
        isRefresh = true;
        presenter.getFriends(getActivity());
    }

    @Override
    public void onLoadMoreRequested() {
        isRefresh = false;
        presenter.getFriends(getActivity());
    }
}
